// Copyright (C) 2005 Taylor L. Riché <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// 	$Id: Scheduler.hh 424 2006-05-26 17:02:27Z richetl $	
//

#ifndef SCHEDULER_HH
#define SCHEDULER_HH

#include <assert.h>
#include <list>
#include <map>
#include <string>

#ifdef LEE_BUILD
#include "Function.hh"
#include "Logger.hh"
#include "PCon.hh"
#include "Synchronized.hh"
#else
#include <lee/Function.hh>
#include <lee/Logger.hh>
#include <lee/PCon.hh>
#include <lee/Synchronized.hh>
#endif

namespace lee {

  class ProcUnit;
  class ApplicationGraph;
  class CourierUnit;
  class PCon;
  class Function;

  class Scheduler : public Synchronized
  {
    std::map<std::string, ProcUnit *> composableMap;
    std::list<CourierUnit *> cuList;
    std::list<CourierUnit *>::iterator currCU;
    std::list<ProcUnit *> * pList;
    std::list<ProcUnit *>::iterator currPU;
    pthread_key_t exitKey;
    pthread_key_t eventKey;
    Logger log;
  public:
    Scheduler() : currCU(cuList.begin()) {}
    ~Scheduler() {}
    ProcUnit* getProcUnit(PCon * pcon);
    void schedule(std::list<ProcUnit *> & p, Container & cont, bool first);
    void registerCourier(CourierUnit * cu) {cuList.push_back(cu);return;}
    void setPU(Function * func);
    CourierUnit * getCU() {
      if(currCU == cuList.end()) {
	currCU = cuList.begin();
      }
      return *currCU;
    }
    void assignNewProc(Function * func);
    inline void setKeys(pthread_key_t exK, pthread_key_t evK) {exitKey = exK;eventKey = evK;return;}
  };
  
}

#endif // SCHEDULER_HH
